#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mtthread.h"
#include "mytack.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
#if 0
    //第一种方法
    Generate  *gen=new Generate(this);
    BubbleSort *Bubble=new BubbleSort(this);
    QuickSolt *quick=new QuickSolt(this);
    connect(gen,&Generate::sendArray,Bubble,&BubbleSort::recArray);
    connect(gen,&Generate::sendArray,quick,&QuickSolt::recArray);
    connect(this,&MainWindow::starting,gen,&Generate::revnum);
    //启动子线程
    connect(ui->Btn1,&QPushButton::clicked,this,[=](){
        emit starting(10000);
         gen->start();
    });
    connect(gen,&Generate::sendArray,this,[=](QVector<int> list){
        Bubble->start();
        quick->start();
        for(int i=0;i<list.size();i++){
            //这里添加项目，这个控件是列表控件，所以对应是一行数据。
            ui->NumList->addItem(QString::number(list.at(i)));
        }
    });
    connect(Bubble,&BubbleSort::finish,this,[=](QVector<int> list){
        for(int i=0;i<list.size();i++){
            ui->BubbleList->addItem(QString::number(list.at(i)));
        }
    });

    connect(quick,&QuickSolt::finish,this,[=](QVector<int> list){
        for(int i=0;i<list.size();i++){
            ui->quickList->addItem(QString::number(list.at(i)));
        }
    });

#else


    //第二种方法
    QThread *task1=new QThread(this);
    QThread *task2=new QThread(this);
    QThread *task3=new QThread(this);
    //任务类不能指定父对象
    Generatet *gen=new Generatet;
    BubbleSortt *bubble=new BubbleSortt;
    QuickSoltt *quick=new QuickSoltt;

    //将任务对象移动到子线程中
    gen->moveToThread(task1);
    bubble->moveToThread(task2);
    quick->moveToThread(task3);

     connect(this,&MainWindow::starting,gen,&Generatet::working);

         connect(gen,&Generatet::sendArray,bubble,&BubbleSortt::working);
         connect(gen,&Generatet::sendArray,quick,&QuickSoltt::working);
      connect(ui->Btn1,&QPushButton::clicked,this,[=](){
          emit starting(10000);
          task1->start();


        });
        connect(gen,&Generatet::sendArray,this,[=](QVector<int> list){
                task2->start();
                task3->start();
                for(int i=0;i<list.size();i++){
                    //这里添加项目，这个控件是列表控件，所以对应是一行数据。
                    ui->NumList->addItem(QString::number(list.at(i)));
                }
            });

        connect(bubble,&BubbleSortt::finish,this,[=](QVector<int> list){
            for(int i=0;i<list.size();i++){
                ui->BubbleList->addItem(QString::number(list.at(i)));
            }
        });

        connect(quick,&QuickSoltt::finish,this,[=](QVector<int> list){
            for(int i=0;i<list.size();i++){
                ui->quickList->addItem(QString::number(list.at(i)));
            }
        });
                connect(this,&MainWindow::destroyed,this,[=](){
                    task1->quit();
                    task1->wait();
                    task1->deleteLater();

                    task2->quit();
                    task2->wait();
                    task2->deleteLater();

                    task3->quit();
                    task3->wait();
                    task3->deleteLater();
                });

#endif



}

MainWindow::~MainWindow()
{
    delete ui;
}

